<?php
namespace app\interfaces\controller;

use app\interfaces\model\MemberDistributionRecords;
use think\Db;
use think\Cache;
use think\Request;
use think\Exception;
use app\interfaces\model\Shop as ShopModel;
use app\interfaces\model\Member as MemberModel;
use app\interfaces\model\MemberOrder as MemberOrderModel;
use app\interfaces\model\MemberRefund as MemberRefundModel;
use app\interfaces\model\MemberCoupon as MemberCouponModel;
use app\interfaces\model\MemberPacket as MemberPacketModel;
use app\interfaces\model\MemberAddress as MemberAddressModel;
use app\interfaces\model\FreightTemplate as FreightTemplateModel;
use app\interfaces\model\MemberOrderAttached as MemberOrderAttachedModel;
use think\Log;
use think\Session;
use think\Cookie;

class Test{
    public function ab(){

        $member_total_price = Db::name('member_distribution_records')
            ->where('shop_order_number','20180917192740630590527660235618')
            ->field('member_id,sum(price) price')
            ->group('member_id')
            ->select();
        echo Db::getLastSql();
        $sum = 0;
        foreach ($member_total_price as $k => $v){
            $sum +=$v['price'];

        }
        halt($sum);

        $shop_order_number = '20180917192740630590527660235618';
        $web_y = db('config')->where('varname','web_y')->value('varvalue');
        $web_e = db('config')->where('varname','web_e')->value('varvalue');
        $web_f = db('config')->where('varname','web_f')->value('varvalue');
        $web_g = db('config')->where('varname','web_g')->value('varvalue');
        $web_gt = db('config')->where('varname','web_gt')->value('varvalue');
        $now_order_goods_count = Db::name('member_order a')
            ->where('a.shop_order_number',$shop_order_number)
            ->join('member_order_attached b','a.shop_order_number = b.shop_order_number')
            ->sum('number');
        $order_find = Db::name('member_order')->where(['order_number|shop_order_number' => $shop_order_number])->find();
        $y = bcmul($now_order_goods_count,$web_y,2);
        $e = bcmul($now_order_goods_count,$web_e,2);
        $f = bcmul(1,$web_f,2);
        $g = bcmul($now_order_goods_count,$web_g,2);
        $gt = bcmul($now_order_goods_count,$web_gt,2);
        $time = date('Y-m-d H:i:s',time());
       // $a = 610;

        $hierarchy = Db::name('member_distribution')->where(['referrals_id' => $order_find['member_id']])->value('hierarchy');
        // 判断是否存在
        if (!empty($hierarchy)) {
            $hierarchy = explode(',', $hierarchy);      // 变成数组
            array_shift($hierarchy);                    // 去掉第一个key
            array_pop($hierarchy);                      // 去掉最后一个key
            $hierarchy = array_slice($hierarchy, -10);   // 倒数3个
            rsort($hierarchy);                       // 逆向排序
        }

        if (!empty($hierarchy)) {
            foreach ($hierarchy as $k => $v) {
                if ($k + 1 == 1) { //$k+1代表当前级别
                    $insert = [
                        'shop_order_number' => $shop_order_number,
                        'member_id' => $v,
                        'type' => 'y',
                        'price' => $y,
                        'create_time' => $time
                    ];
                    $member_y = Db::name('member')->where('id', $v)->fetchSql(true)->setInc('y', $y);
                    halt($member_y);
                    //$y_record = Db::name('member_distribution_records')->insert($insert);
//                    if($y_record){
//
//
//                    }
                }

                ///

//                    if ($member_order_goods_count == $k + 1) {
//                        Db::name('member')->where('id', $v)->setInc('f', $f);
//                        $insert = [
//                            'shop_order_number' => $shop_order_number,
//                            'member_id' => $v,
//                            'type' => 'f',
//                            'price' => $f,
//                            'create_time' => $time
//                        ];
//                        Db::name('member_distribution_records')->insert($insert);
//                    }

            }
        }
}
}

